public class SolutionInterview17_11 {
    public int findClosest(String[] words, String word1, String word2) {
        int a=-1,b=-1,ans=Integer.MAX_VALUE;
        for (int i=0;i<words.length;i++) {
            if (words[i].equals(word1)) {
                a=i;
                if (b!=-1){
                    ans=Math.min(ans,a-b);
                }
            }else if (words[i].equals(word2)){
                b=i;
                if (a!=-1){
                    ans=Math.min(ans,b-a);
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview17_11().findClosest(new String[]{"I","am","a","student","from","a","university","in","a","city"},"a","student"));
    }
}
